//
//  WHCategoryScrollController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WHCategoryScrollController.h"
#import "WSAppTools.h"
#import "WSModel.h"
#import "WHCategoryScrollView.h"
#import "WSAlbumModel.h"
#import "WHCategoryTableViewCell.h"
#import "WSAlbumsDetailViewController.h"
#import <BmobSDK/Bmob.h>

@interface WHCategoryScrollController ()<UITableViewDataSource,UITableViewDelegate,WHCategoryScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *titleMoreArray;
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@property (nonatomic, retain) WHCategoryScrollView *moreView;
@property (nonatomic, retain) WHCategoryTableView *moreTableView;
@property (nonatomic, assign) NSInteger currentTag;
@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic,assign) BOOL isUploading;
@property (nonatomic, assign) NSInteger currentPage;

@property (nonatomic, strong) NSMutableArray *collectArr;
@end

@implementation WHCategoryScrollController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    //改变title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil]];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.title = self.moreTitleS;
    //渲染取消
    UIImage *leftImage = [UIImage imageNamed:@"btn_back_n"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    /**
     *  数组初始化
     */
    self.titleMoreArray = [NSMutableArray array];//存放标题
    self.tableViewArray = [NSMutableArray array];//存放tableView
    self.collectArr = [NSMutableArray array];//
    
    
    /**
     *  请求标题
     */
    [self getWorking:self.moreTitleId];
    
    self.currentPage = 1;
    /**
     *  小菊花
     */
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"更多 • 加载";
    


    self.moreView = [[WHCategoryScrollView alloc] initWithFrame:CGRectMake(0, 0, WID*375, HEI*667)];
    self.moreView.WHCategorydelegate = self;
}
//导航栏左侧返回
-(void)leftAction:(UIButton *)btn{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark -MoreView协议方法

-(void)titleButtonAction:(UIButton *)button{
    
    self.currentTag = button.tag - 100;
    
    self.moreTableView = [self.tableViewArray objectAtIndex:self.currentTag];
    
    
}
//取TableView
-(void)endDraggingAction:(NSInteger)tag{
    
    NSLog(@"%ld", tag);
    
    self.currentTag = tag;
    
    self.moreTableView = [self.tableViewArray objectAtIndex:self.currentTag];
    
    [self initTableView];
    
}
#pragma mark - 网络请求title

-(void)getWorking:(NSString *)categoryId{
    
    NSString *name = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%@&contentType=album&device=iPhone",categoryId];
    
    
    [WSAppTools getDataWithURL:name block:^(id result) {
        NSDictionary *dic = result;
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            
            WSModel *model = [WSModel modelWithDictionary:dic];
            [self.titleMoreArray addObject:model.tname];
            
        }
        if (self.moreTableView.dataArray.count > 0){
            
            return ;
        }
        
        [self.view addSubview:self.moreView];
        //将title传进moreView
        [self.moreView setTitleContent:self.titleMoreArray];
        for (int i = 0; i < self.titleMoreArray.count; i++) {
            
            WHCategoryTableView *tableView = [[WHCategoryTableView alloc] initWithFrame:CGRectMake(0, 0, WID*375, HEI*667) style:UITableViewStylePlain];
            [self.tableViewArray addObject:tableView];
            
        }
        //协议传tableView的数组
        [self.moreView setBgScrollViewContents:self.tableViewArray];
        self.moreTableView = [self.tableViewArray objectAtIndex:0];
        [self initTableView];
        [self.hub hide:YES];
        [self.moreTableView reloadData];
    } error:^(NSError *error) {
        
        
    }];
    
}
#pragma mark - 网络请求tname

-(void)getTnamedataId:(NSString *)catId tagName:(NSString *)name pageID:(NSInteger)page{
    /**
     *  判断数组是否为空,否则会再走此方法,数组里对象重复
     */
    
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%@&device=iPhone&pageId=%ld&pageSize=20&status=0&tagName=%@", catId, page,name];
#pragma mark - 查询收藏列表,给Model赋值

    BmobQuery *bquery = [BmobQuery queryWithClassName:@"Collect_album"];
    //查找GameScore表的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        self.collectArr = [NSMutableArray arrayWithArray:array];
        
        [WSAppTools getDataWithURL:url block:^(id result) {
            
            if (self.isUploading == NO){
                
                [self.moreTableView.dataArray removeAllObjects];
                
            }
            
            //NSLog(@"%@",result);
            NSDictionary *dic = result;
            NSArray *arrayList = [dic objectForKey:@"list"];
            for (NSDictionary *dicList in arrayList) {
                
                WSAlbumModel *moreList = [WSAlbumModel modelWithDictionary:dicList];
                moreList.isFavor = NO;
                
                for (BmobObject *objc in self.collectArr) {
                    
                    if ([[objc objectForKey:@"albumName"] isEqualToString:moreList.title]) {
                        
                        moreList.objectId = [objc objectId];
                        moreList.isFavor = YES;
  
                    }
                    
                }
                [self.moreTableView.dataArray addObject:moreList];
            }
            
            
            
            [self.moreTableView.mj_header endRefreshing];
            [self.moreTableView.mj_footer endRefreshing];
            [self.moreTableView reloadData];
        } error:^(NSError *error) {
            
            
        }];

    }];

    
    
}

#pragma mark - 收藏

-(void)collectAlbum:(NSMutableArray *)array{
    
    
    
    
    
}


//tableView初始化
-(void)initTableView{
    
    if (self.moreTableView.dataArray.count > 0){
        return;
    }
    self.moreTableView.delegate = self;
    self.moreTableView.dataSource = self;
    [self.moreTableView registerClass:[WHCategoryTableViewCell class] forCellReuseIdentifier:@"WHCategoryTableViewCell"];
    self.moreTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    self.moreTableView.mj_header = header;
    [self.moreTableView.mj_header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downTableView)];
    self.moreTableView.mj_footer = footer;
    
}
#pragma mark - 刷新
-(void)upTableView{
    
    NSLog(@"下拉");
    self.currentPage = 1;
    self.isUploading = NO;
    
    [self getTnamedataId:self.moreTitleId tagName:[self.titleMoreArray objectAtIndex:self.currentTag] pageID:self.currentPage];
    
    
}
-(void)downTableView{
    
    
    self.isUploading = YES;
    self.currentPage++;
    [self getTnamedataId:self.moreTitleId tagName:[self.titleMoreArray objectAtIndex:self.currentTag] pageID:self.currentPage];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.moreTableView.dataArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndexM = @"WHCategoryTableViewCell";
    WHCategoryTableViewCell *cellM = [tableView dequeueReusableCellWithIdentifier:cellIndexM];
    //
    WSAlbumModel *list = [self.moreTableView.dataArray objectAtIndex:indexPath.row];
    //
    cellM.albumModel = list;
    
    return cellM;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 100*WID;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
    albumsDetailVC.navigationController.navigationBar.translucent = YES;
//    [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
    WSAlbumModel *list = [self.moreTableView.dataArray objectAtIndex:indexPath.row];
    albumsDetailVC.albumId = list.myId;
    [self.navigationController pushViewController:albumsDetailVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
